// pages/TGgoods/TGgoods.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
import Common from "../../utils/common.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 搜索参数
    searchInfo: {
      page: 1,
      search: '',
      cate_id: '',
      type: 1
    },
    // 下拉框
    selectShow: false,
    selectName1: '',
    selectList1: [],
    //卡片列表
    list: [],
    //已触底
    chudi: false,
    jiazai: false,
    // 快捷进入
    quick: false,
    title: '',
    quickList:[],
    clickQuick: null,
    clickIndex: null,
    goods_id: null,
    baseInfo: null,
    background: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      quick:options.quick,
      title:options.title,
    })
    let str = "searchInfo.longitude"
    let str1 = "searchInfo.latitude"
    this.setData({
      [str]: getApp().globalData.longitude,
      [str1]: getApp().globalData.latitude,
    })
    this.getTabInfo()
    this.wpxSearch()
  },


  //搜索
  toSearch(e) {
    this.setData({
      searchInfo: {
        page: 1,
        type: 4
      }
    })
    let str = "searchInfo.search"
    this.setData({
      [str]: e.detail.value
    })
    let Data = this.data.searchInfo
    JsonPost('goods/search', Data).then(res => {
      this.setData({
        list: [...this.data.list, ...res.data.data],
      })
      if (res.data.data.length == 0) {
        this.setData({
          chudi: true,
          jiazai: false
        })
      } else {
        this.setData({
          chudi: false
        })
      }
    })
  },

  wpxSearch() {
    let Data = this.data.searchInfo
    JsonPost('goods/search', Data).then(res => {
      this.setData({
        list: [...this.data.list, ...res.data.data],
      })
      if (res.data.data.length == 0) {
        this.setData({
          chudi: true,
          jiazai: false
        })
      } else {
        this.setData({
          chudi: false
        })
      }
    })
  },



  // 进入查询卡片类别
  getTabInfo() {
    JsonPost('category/list').then(res => {
      this.setData({
        selectList1: res.data
      })
      if (this.data.quick=='true') {
        res.data.forEach(ele => {
          if (ele.name==this.data.title) {
            this.setData({
              quickList:ele.son,
            })
          }
        });
      }
    })
  },

  // 点击分类
  tapType(e) {
    let str = 'searchInfo.cate_id'
    let str1 = 'searchInfo.search'
    this.setData({
      [str]: e.currentTarget.dataset.id,
      [str1]: '',
      list: [],
      quick:false,
      selectShow: !this.data.selectShow
    })
    this.wpxSearch()
  },

  // 点击品牌
  tapBrand(e) {
    let str = 'searchInfo.search'
    let str1 = 'searchInfo.cate_id'
    let fuIndex = e.currentTarget.dataset.findex
    let index = e.currentTarget.dataset.index
    this.setData({
      [str]: e.currentTarget.dataset.name,
      [str1]: e.currentTarget.dataset.fid,
      clickIndex: fuIndex + 'wpx' + index,
      list: [],
      quick:false,
      selectShow: !this.data.selectShow
    })
    this.wpxSearch()
  },


  // 点击条件
  tapTerm(e){
    let id= Number(e.currentTarget.dataset.id)
    if (this.data.searchInfo.type==3&&id==3) {
      id=2
    }
    let str = 'searchInfo.type'
    this.setData({
      [str]:id,
      list:[]
    })
    this.wpxSearch()
  },

  // 快捷点击
  tapQuick(e){
    let str='searchInfo.search'
    this.setData({
      clickQuick:e.currentTarget.dataset.index,
      [str]:this.data.quickList[e.currentTarget.dataset.index].name
    })
    this.wpxSearch()
  },



  //选择table栏
  tabAction() {
    this.setData({
      selectShow: !this.data.selectShow
    })
  },

  //前往商品详情页
  goDetail(e) {
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goods_id=' + e.currentTarget.dataset.row.id
    })
  },

  
  share(e) {
    this.setData({
      goods_id:e.currentTarget.dataset.row.id,
      baseInfo:e.currentTarget.dataset.row,
      background:e.currentTarget.dataset.row.img,
    })
    this.selectComponent("#vposter").creat()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.chudi) {
      let str = "searchInfo.page"
      this.setData({
        [str]: this.data.searchInfo.page + 1,
        jiazai: true
      })
      this.wpxSearch()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    if (options.from === 'button') {
      // 来自页面内转发按钮
      let shareObj = {
        title: this.data.goods_name,
        path: "/pages/goodsDetail/goodsDetail?goods_id=" + this.data.goods_id,
        imageUrl: this.data.background[1]
      }
      return shareObj;
    }
  }
})